package Init

import (
	"github.com/spf13/viper"
	"database/sql"
	"gorm.io/gorm"
	"github.com/gin-contrib/sessions/redis"
)

// 全局变量
var (
	BaseCfg     *BaseConf
	MysqlMapCfg *MysqlMapConf
	//ConfRedis    *RedisConf
	RedisMapCfg *RedisMapConf
	EtcdCfg     *EtcdConf

	DBMapPool       map[string]*sql.DB
	GormMapPool     map[string]*gorm.DB
	RedisStore      *redis.Store
	DBDefaultPool   *sql.DB
	GormDefaultPool *gorm.DB

	ViperConfMap map[string]*viper.Viper // 保存解析完各个配置文件后的 viper 对象
)

type BaseConf struct {
	DebugMode    string  `mapstructure:"debug_mode"`
	TimeLocation string  `mapstructure:"time_location"`
	Log          LogConf `mapstructure:"log"`
	Base         struct {
		DebugMode    string `mapstructure:"debug_mode"`
		TimeLocation string `mapstructure:"time_location"`
	} `mapstructure:"base"`
}

type LogConfFileWriter struct {
	On              bool   `mapstructure:"on"`
	LogPath         string `mapstructure:"log_path"`
	RotateLogPath   string `mapstructure:"rotate_log_path"`
	WfLogPath       string `mapstructure:"wf_log_path"`
	RotateWfLogPath string `mapstructure:"rotate_wf_log_path"`
}

type LogConfConsoleWriter struct {
	On    bool `mapstructure:"on"`
	Color bool `mapstructure:"color"`
}

type LogConf struct {
	Level   string               `mapstructure:"log_level"`
	Tracing bool                 `mapstructure:"tracing"`
	FW      LogConfFileWriter    `mapstructure:"file_writer"`
	CW      LogConfConsoleWriter `mapstructure:"console_writer"`
}

type MysqlMapConf struct {
	List map[string]*MySQLDBConf `mapstructure:"list"`
}

type MySQLDBConf struct {
	DriverName      string `mapstructure:"driver_name"`
	DataSourceName  string `mapstructure:"data_source_name"`
	DataSourceRead  string `mapstructure:"data_source_read"`
	MaxOpenConn     int    `mapstructure:"max_open_conn"`
	MaxIdleConn     int    `mapstructure:"max_idle_conn"`
	MaxConnLifeTime int    `mapstructure:"max_conn_life_time"`
}

type RedisMapConf struct {
	List map[string]*RedisDBConf `mapstructure:"list"`
}

type RedisDBConf struct {
	ProxyList    []string `mapstructure:"proxy_list"`
	Password     string   `mapstructure:"password"`
	Db           int      `mapstructure:"db"`
	ConnTimeout  int      `mapstructure:"conn_timeout"`
	ReadTimeout  int      `mapstructure:"read_timeout"`
	WriteTimeout int      `mapstructure:"write_timeout"`
}

type EtcdConf struct {
	Hosts       []string `mapstructure:"hosts"`
	DialTimeout int      `mapstructure:"dial_timeout"`
}
